// 文章列表页面

<template>
  <div class="postList">
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card" style="margin-top:20px">
      <!-- 表格 -->
      <el-table :data="postList" border style="width: 100%">
        <el-table-column type="index" width="30"></el-table-column>
        <el-table-column prop="title" label="标题" width="180"></el-table-column>
        <el-table-column prop="type" label="类型" width="180">
          <!-- 自定义列表 -->
          <template slot-scope="scope">{{scope.row.type=1?'文章':'视频'}}</template>
        </el-table-column>
        <el-table-column prop="user.nickname" label="作者"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="success"
              icon="el-icon-share"
              @click="handleShare(scope.row)"
            >分享</el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页结构 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="2"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getPostList } from "@/apis/article";
export default {
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageIndex = val;
      this.init();
    },
    handleEdit(row) {
      console.log(row);
    },
    handleShare(row) {
      console.log(row);
    },
    handleDelete(row) {
      console.log(row);
    },
    // 封装初始化页面
    async init(){
      let res = await getPostList({
      pageSize: this.pageSize,
      pageIndex: this.pageIndex,
    });
    console.log(res);
    this.postList = res.data.data;
    this.total = res.data.total;
    }
  },
  data() {
    return {
      postList: [], // 文章列表数据
      pageSize: 2,
      pageIndex: 1,
      total:0
    };
  },
  mounted() {
    this.init();
  },
};
</script>

<style lang="less" scoped>
</style>